package com.android.huimeiqupin.fragment.member;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.huimeiqupin.R;
import com.android.huimeiqupin.adapt.GridDetaillikeAdapt;
import com.android.huimeiqupin.fragment.BaseLazyFragment;
import com.android.huimeiqupin.model.detail.SearchLogisticsData;
import com.android.huimeiqupin.model.detail.SearchLogisticsDataCallback;
import com.android.huimeiqupin.model.group.MainList;
import com.android.huimeiqupin.model.group.MainListCallback;
import com.android.huimeiqupin.utils.ConstantsUrl;
import com.android.huimeiqupin.utils.HttpUtils;
import com.android.huimeiqupin.utils.ObjectUtils;
import com.android.huimeiqupin.view.MyGridView;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 物流详情页
 */
public class Fragment_Mine_Express extends BaseLazyFragment {


    @BindView(R.id.top)
    View top;
    @BindView(R.id.wuliu_list)
    NestFullListView wuliuList;
    @BindView(R.id.detail_like_grid)
    MyGridView detailLikeGrid;
    @BindView(R.id.scroll)
    ScrollView scroll;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.wuliu_kuaidiname)
    TextView wuliu_kuaidiname;
    @BindView(R.id.wuliu_kuaidinumber)
    TextView wuliu_kuaidinumber;
    @BindView(R.id.wuliu_kuaidiphone)
    TextView wuliu_kuaidiphone;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;
    private ArrayList<MainList.MainListdata.Mainlistdata> listdata = new ArrayList<>();
    private boolean networkConnected;
    private static final String ARG_PARAM1 = "param1";
    private String order_sn;
    private GridDetaillikeAdapt adapter;

    public Fragment_Mine_Express() {

    }

    public static Fragment_Mine_Express newInstance(String need_id) {
        Fragment_Mine_Express fragment = new Fragment_Mine_Express();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, need_id);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            order_sn = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        initExpress();
    }

    @Override
    protected void onInvisible() {

    }

    @Override
    protected void initData() {
        GetDataListData("&typeid=1");
        GetLogisticList();
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_mine_order_express, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    private ArrayList<String> list_express = new ArrayList<>();

    private void initExpress() {
        scroll.smoothScrollTo(0, 0);
        top.setFocusable(true);
        top.setFocusableInTouchMode(true);
        top.requestFocus();
        adapter = new GridDetaillikeAdapt(mcontext, listdata);
        detailLikeGrid.setAdapter(adapter);
        detailLikeGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ObjectUtils.ToDetailActivity(mcontext, 1, listdata.get(position).getId(), "");
            }
        });

        wuliuList.setAdapter(new NestFullListViewAdapter<String>(R.layout.item_express, list_express) {
            @Override
            public void onBind(int position, String testBean, NestFullViewHolder holder) {
                holder.setText(R.id.item_express_tv, testBean);
            }
        });
    }

    //获取列表数据
    private void GetDataListData(String type) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MAIN_LISTDATA + "?size=" + 8 + type;
            HttpUtils.get(address, null, new MainListCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MainList response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    listdata.clear();
                    MainList.MainListdata data = response.getData();
                    if (data != null && ObjectUtils.listHasData(data.getList())) {
                        listdata.addAll(data.getList());
                        adapter.notifyDataSetChanged();
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    public void GetLogisticList() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.SEARCHLOGISTICS;
            HashMap<String, String> map = new HashMap<>();
            map.put("order_sn", order_sn);
            HttpUtils.get(address, map, new SearchLogisticsDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SearchLogisticsData response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    SearchLogisticsData.DataBean data = response.getData();
                    if (data != null) {
                        if (data.getExpress() != null) {
                            wuliu_kuaidiname.setText(data.getExpress());
                        }
                        if (data.getContact_mobile() != null) {
                            wuliu_kuaidiphone.setText(data.getContact_mobile());
                        }
                        if (data.getExpress_num() != null) {
                            wuliu_kuaidinumber.setText(data.getExpress_num());
                        }

                        if (ObjectUtils.listHasData(data.getWuliu())) {
                            List<SearchLogisticsData.DataBean.LogisticBean> wuliu = data.getWuliu();
                            for (int i = 0; i < wuliu.size(); i++) {
                                list_express.add(wuliu.get(i).getTime() + wuliu.get(i).getContext());
                            }
                            wuliuList.updateUI();
                        }
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        GetLogisticList();
        GetDataListData("&typeid=1");
    }
}